<template>
	<div class="div1">
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>咨询师管理</el-breadcrumb-item>
		</el-breadcrumb>
	</div>
	<div>
		<div class="div2">
			<span class="spanTop">咨询师名称</span>
			<el-input size="large" v-model="searchName" style="width: 240px" placeholder="请输入咨询师名称" />
			<span class="spanTop">咨询师等级</span>
			<el-select v-model="searchCounselorLevelName" placeholder="请选择咨询师等级" size="large" style="width: 240px">
				<el-option v-for="(item,index) in AllCounselorLevel" :value="index+1"
					:label="item.counselorLevelName" />
			</el-select>
			<span class="spanTop">地址</span>
			<el-input size="large" v-model="searchAddress" style="width: 240px" placeholder="请输入地址" />
			<span class="spanTop">手机号</span>
			<el-input size="large" v-model="searchPhone" style="width: 240px" placeholder="请输入手机号" />
		</div>
		<el-button @click="query" type="primary" size="large" class="bt1">搜索</el-button>
		<el-button type="primary" @click="createOrUpdate('')" size="large" class="bt1">新增咨询师</el-button>
		<el-button type="danger" size="large" class="bt1" @click="removeAll">批量删除</el-button>
		<el-table :data="tableData" style="width: 100%;margin-top: 10px;" border 
		@selection-change="handleSelectionChange">
			<el-table-column type="selection" />
			<el-table-column fixed prop="id" label="ID" />
			<el-table-column prop="counselorName" label="咨询师名称" />
			<el-table-column prop="counselorLevel.counselorLevelName" label="咨询师等级" />
			<el-table-column prop="counselorPhone" label="手机号" />
			<el-table-column prop="counselorEmail" label="邮箱" />
			<el-table-column label="头像">
				<template #default="scope">
					<img :src="scope.row.counselorImg" alt="头像">
				</template>
			</el-table-column>
			<el-table-column label="证书图片">
				<template #default="scope">
					<img :src="scope.row.counselorCertificate" alt="证书图片">
				</template>
			</el-table-column>
			<el-table-column prop="counselorAddress" label="所属地址" />
			<el-table-column prop="counselorProfile" label="咨询师简介" />
			<el-table-column label="职位">
				<template #default="scope">
					<div class="counselorPostsOne" v-if="scope.row.counselorPosts==1">咨询师</div>
					<div class="counselorPostsTwo" v-else>心理导师</div>
				</template>
			</el-table-column>

			<el-table-column fixed="right" label="操作" width="200">
				<template #default="scope">
					<el-button type="primary" @click="createOrUpdate(scope.row)">编辑</el-button>
					<el-button type="danger" @click="remove(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-dialog v-model="dialogFormVisible" :title="form.id.length==0?'新增咨询师':'编辑咨询师'" width="800"
			style="margin-bottom: 60px;" @close="handleClose">
			<el-form>
				<el-form-item label="咨询师名称" :label-width="formLabelWidth">
					<el-input size="large" v-model="form.counselorName" autocomplete="off" placeholder="请输入咨询师名称" />
				</el-form-item>

				<el-form-item label="咨询师等级" :label-width="formLabelWidth" style="clear: left;">
					<el-select v-model="form.counselorLevelId" placeholder="请选择咨询师等级" size="large" style="width: 240px">
						<el-option v-for="(item,index) in AllCounselorLevel" :value="index+1"
							:label="item.counselorLevelName" />
					</el-select>
				</el-form-item>
				<el-form-item label="手机号" :label-width="formLabelWidth">
					<el-input size="large" v-model="form.counselorPhone" autocomplete="off" placeholder="请输入手机号" />
				</el-form-item>
				<el-form-item label="邮箱" :label-width="formLabelWidth">
					<el-input size="large" v-model="form.counselorEmail" autocomplete="off" placeholder="请输入邮箱" />
				</el-form-item>
				<el-form-item label="头像" :label-width="formLabelWidth">
					<el-upload class="avatar-uploader"
						action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15" :show-file-list="false"
						:on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
						<img v-if="imageUrl" :src="imageUrl" class="avatar" />
						<el-icon v-else class="avatar-uploader-icon">
							<Plus />
						</el-icon>
					</el-upload>
				</el-form-item>
				<el-form-item label="证书图片" :label-width="formLabelWidth">
					<el-upload class="avatar-uploader"
						action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15" :show-file-list="false"
						:on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
						<img v-if="imageUrl" :src="imageUrl" class="avatar" />
						<el-icon v-else class="avatar-uploader-icon">
							<Plus />
						</el-icon>
					</el-upload>
				</el-form-item>
				<el-form-item label="所属地址" :label-width="formLabelWidth">
					<el-input size="large" v-model="form.counselorAddress" autocomplete="off" placeholder="请上传所属地址" />
				</el-form-item>
				<el-form-item label="咨询师简介" :label-width="formLabelWidth">
					<el-input size="large" v-model="form.counselorProfile" autocomplete="off" placeholder="请上传咨询师简介" />
				</el-form-item>
				<el-form-item label="咨询师分类" :label-width="formLabelWidth" style="clear: left;">
					<el-select v-model="form.counselorPosts" placeholder="请选择咨询师分类" size="large" style="width: 240px">
						<el-option :value="1" label="咨询师" />
						<el-option :value="2" label="心理导师" />
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="dialogFormVisible = false">取消</el-button>
					<el-button @click="dialogFormVisible = false,add()">
						确定
					</el-button>
				</div>
			</template>
		</el-dialog>
		<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10,15, 20]"
			:size="size" layout="total, sizes, prev, pager, next, jumper" :total=" total"
			@size-change="handleSizeChange" @current-change="handleCurrentChange" v-if="total !=0" />
	</div>
</template>
<script setup>
	import {
		onMounted,
		ref,
		reactive,
		computed,
		watch
	} from 'vue'
	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'
	import {
		Plus
	} from '@element-plus/icons-vue'
	import {
		getCurrentInstance
	} from 'vue';

	const axios = getCurrentInstance().appContext.config.globalProperties.axios
	const tableData = ref([])
	const currentPage = ref(1) // 当前页
	const pageSize = ref(5) // 每页显示条目数
	const size = 'default' // 分页大小
	const total = ref(0)
	const searchName = ref('')
	const searchCounselorLevelName = ref('')
	const searchAddress = ref('')
	const searchPhone = ref('')

	const form = ref({
		counselorAddress: "",
		counselorCertificate: "",
		counselorEmail: "",
		counselorImg: "",
		counselorLevelId: 1,
		counselorName: "",
		counselorPhone: "",
		counselorPosts: 1,
		counselorProfile: "",
		id: "",
	})

	const dialogFormVisible = ref(false)
	const formLabelWidth = '100px'
	const AllCounselorLevel = ref([])

	const getAllCounselorLevel = () => {
		axios.get('counselorLevel', ).then((resp) => {
			console.log(resp.data);
			AllCounselorLevel.value = resp.data
		});
	}

	function query() {
		axios.get(
			`counselor/page?counselorName=${searchName.value}&counselorLevelId=${searchCounselorLevelName.value}&counselorAddress=${searchAddress.value}&counselorPhone=${searchPhone.value}&pageNum=${currentPage.value}&pageSize=${pageSize.value}`,
		).then(resp => {
			tableData.value = resp.data.list;
			total.value = resp.data.total
			console.log(resp);
		})
	}

	const handleSizeChange = (val) => {
		pageSize.value = val;
		query();
	}
	const handleCurrentChange = (val) => {
		currentPage.value = val
		query();
	}
	const handleClose = () => {
		form.value = {
			counselorAddress: "",
			counselorCertificate: "",
			counselorEmail: "",
			counselorImg: "",
			counselorLevelId: 1,
			counselorName: "",
			counselorPhone: "",
			counselorPosts: 1,
			counselorProfile: "",
			id: '',
		}
	}
	const createOrUpdate = (row) => {
		dialogFormVisible.value = true
		if (row != '') {
			axios.get(`counselor/${row.id}`).then((resp) => {
				console.log('点击编辑得到的：', resp);
				form.value = resp.data
			})
		} else {
			console.log('点击新增');
		}
	}
	const add = () => {
		console.log("新增的id", form.value.id);
		if (form.value.id.length === 0) {
			axios.post('/counselor', {
				counselorAddress: form.value.counselorAddress,
				counselorCertificate: form.value.counselorCertificate,
				counselorEmail: form.value.counselorEmail,
				counselorImg: form.value.counselorImg,
				counselorLevelId: form.value.counselorLevelId,
				counselorName: form.value.counselorName,
				counselorPhone: form.value.counselorPhone,
				counselorPosts: form.value.counselorPosts,
				counselorProfile: form.value.counselorProfile,
				id: '',
			}).then((resp) => {
				console.log('触发新增请求', resp);
				if (resp.data.code == 200) {
					ElMessage({
						message: '新增成功',
						type: 'success',
					})
				} else {
					ElMessage.error('新增失败')
				}
			}).finally(() => {
				query();
			});
		} else {
			axios.put('counselor', {
				counselorAddress: form.value.counselorAddress,
				counselorCertificate: form.value.counselorCertificate,
				counselorEmail: form.value.counselorEmail,
				counselorImg: form.value.counselorImg,
				counselorLevelId: form.value.counselorLevelId,
				counselorName: form.value.counselorName,
				counselorPhone: form.value.counselorPhone,
				counselorPosts: form.value.counselorPosts,
				counselorProfile: form.value.counselorProfile,
				id: form.value.id,
			}).then((resp) => {
				console.log('触发修改请求', resp);
				if (resp.data.code == 200) {
					ElMessage({
						message: '修改成功',
						type: 'success',
					})
				} else {
					ElMessage.error('修改失败')
				}
			}).finally(() => {
				query();
			});
		}
	}
	const remove = (row) => {
		console.log('当前行的数据', row);
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'Warning', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			)
			.then(() => {
				axios.delete(`counselor/${row.id}`).then((resp) => {
					console.log('删除后：', resp);
					if (resp.data.code == 200) {
						ElMessage({
							message: '删除成功',
							type: 'success',
						})
					} else {
						ElMessage.error('删除失败')
					}
				}).finally(() => {
					query();
				});

			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})

	}
	const multipleSelection = ref([])
	const handleSelectionChange = (val) => {
		multipleSelection.value = val.map(item => {
			return item.id
		})
		console.log(multipleSelection.value);
	}
	const removeAll = (row) => {
		console.log('当前行的数据', row);
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'Warning', {
					confirmButtonText: '确认',
					cancelButtonText: '取消',
					type: 'warning',
				}
			)
			.then(() => {
				axios.delete(`counselor`, {
					params: {
						ids: multipleSelection.value.join(',')
					}
				}).then(resp => {
					if (resp.code == 200) {
						ElMessage({
							message: '删除成功',
							type: 'success',
						})
					} else {
						ElMessage.error('删除失败')
					}
				}).finally(() => {
					query();
				});
			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})
	}
	onMounted(() => {
		query();
		getAllCounselorLevel();
	})
</script>
<style scoped>
	.counselorPostsOne {
		width: 78px;
		background-color: #ecf5ff;
		border: 1px solid #d9ecff;
		color: #44a0ff;
		text-align: center;
		border-radius: 5px;
		padding: 5px 10px;
	}

	.counselorPostsTwo {
		width: 78px;
		background-color: #fef0f0;
		border: 1px solid #fde2e2;
		color: #f56d6d;
		text-align: center;
		border-radius: 5px;
		padding: 5px 10px;
	}

	.spanTop {
		margin: 0 10px;
		color: #696a6e;
	}

	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}

	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}

	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}

	.bt1 {
		margin-top: 10px;
		margin-left: 10px;
	}

	.div1 {
		border-bottom: 1px solid #eeeeee;
		width: 100%;
		height: 30px;
		padding-top: 15px;
		padding-left: 15px;
	}
	.div2{
		padding-top: 15px;
	}
	.el-table{
		margin-left: 10px;
	}
	.el-pagination{
		margin-top: 10px;
		margin-left: 10px;
	}
</style>